#include "httpserver.h"
#include <QtGui>
#include <QtNetwork>

httpServer::httpServer()
{
    ServerName = "";
    ServerPort = 8080;
    tcpServer = new QTcpServer(this);
    tcpSocket = new QTcpSocket(this);
}

bool httpServer::startServer()
{
    tcpServer = new QTcpServer(this);
    if (!tcpServer->listen(QHostAddress::Any, ServerPort))
    {
        QMessageBox::critical(0,"Server",
                              QString("Failure with Server: ").append(tcpServer->errorString())
                              );
        tcpServer->close();
        return false;
    }
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(Connect()));
    return true;
}

bool httpServer::Connect()
{
    tcpSocket = tcpServer->nextPendingConnection();
    connect(tcpSocket, SIGNAL(disconnected()), tcpSocket, SLOT(deleteLater()));
    emit newConnection();
    return true;
}

bool httpServer::Disconnect()
{
    if(tcpSocket->ConnectedState == 0) return false;
    tcpSocket->disconnectFromHost();
    tcpSocket = new QTcpSocket(this);
    return true;
}

bool httpServer::SendSite(QString str)
{
    QByteArray block;
    QString string;
    block.append(QString("HTTP/1.0\r\n"
                        "Content-Type: text/html; charset=\"utf-8\"\r\n"
                        "\r\n")
                        .append(str).toUtf8() );
    tcpSocket->write(block);
    return true;
}
